﻿using System.Collections.Generic;
using Engine;
using Game;
using static Mekiasm.MekCraftingRecipe;
namespace Mekiasm
{
    public class SteelChest : ItemCube
    {
        public static int Index;
        public SteelChest() : base("钢质箱子")
        {
            
        }
        public override int GetFaceTextureSlot(int face, int value)
        {
            return 41;
        }
        public override bool OnUse(Ray3 aim, ComponentMiner componentMiner, TerrainRaycastResult? raycast)
        {
            return false;
        }
        public override void GetDropValues(SubsystemTerrain subsystemTerrain, int oldValue, int newValue, int toolLevel, List<BlockDropValue> dropValues, out bool showDebris)
        {
            SaveItemSystem.GetBlockData(oldValue, out Common.SaveItem toolSaveItem);
            bool isEmpty = true;
            int value = oldValue;
            if (toolSaveItem != null)
            {
                for (int i = 0; i < toolSaveItem.SlotsCount; i++)
                {
                    if (toolSaveItem.GetSlotCount(i) > 0)
                    {
                        isEmpty = false;
                        break;
                    }
                }
                if (isEmpty)//为空值，还原为初始值
                {
                    SaveItemSystem.RemoveBlockData(oldValue, toolSaveItem);
                    value = ILibrary.GetBasicValue(oldValue);
                }
            }
            else
            {//没有saveItem，还原为初始值
                value = ILibrary.GetBasicValue(oldValue);
            }
            showDebris = true;
            dropValues.Add(new BlockDropValue() { Value = value, Count = 1 });
        }

        public override void OnBlockAdded(SubsystemBlockBehavior blockBehavior, int value, int oldValue, int x, int y, int z)
        {
            SaveItemSystem.GetBlockData(value, out Common.SaveItem saveItem);
            if (saveItem == null)
            {
                int newValue = SaveItemSystem.StoreBlockData(value, 0,0, 16);
                ILibrary.SubsystemTerrain.ChangeCell(x, y, z, newValue);
            }
        }

        public override bool OnInteract(SubsystemBlockBehavior blockBehavior, TerrainRaycastResult raycastResult, ComponentMiner componentMiner)
        {
            int value = ILibrary.SubsystemTerrain.Terrain.GetCellValueFast(raycastResult.CellFace.X, raycastResult.CellFace.Y, raycastResult.CellFace.Z);
            SaveItemSystem.GetBlockData(value, out Common.SaveItem saveItem);
            if (saveItem != null)
            {
                componentMiner.ComponentPlayer.ComponentGui.ModalPanelWidget = new SteelChestWidget(saveItem, componentMiner);
            }
            return base.OnInteract(blockBehavior, raycastResult, componentMiner);
        }


    }
}
